﻿using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Permission;
using RedDragon.RBAC.API.Application.Common.UserRolePermission;
using RedDragon.RBAC.ErrorCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.User
{
    public class QueryUserHandler : IRequestHandler<QueryUserCommand, Result>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 用户仓储
        /// </summary>
        public readonly IBaseRepository<Users> usersRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<CreatePermissionsHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="usersRepository"></param>
        /// <param name="logger"></param>
        public QueryUserHandler(ISqlSugarClient db,IBaseRepository<Users> usersRepository, ILogger<CreatePermissionsHandler> logger)
        {
            this.db = db;
            this.usersRepository = usersRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(QueryUserCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = db.Queryable<Users>().Select(d => new UserDto
                {
                    UserId = d.UserId,
                    UserName = d.UserName,
                    UserPwd=d.UserPwd,
                    UserStateName = d.UserState==0 ? "有效" : "无效",

                }).ToList();
                if (list != null)
                {
                    result.Data = list;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "用户列表获取成功";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("用户列表获取异常");
                result.Msg = "用户列表获取异常";
            }
            return result;
        }
    }
    public class UserDto {
        public int UserId { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
     
        public string? UserName { get; set; }
        /// <summary>
        /// 用户密码
        /// </summary>
      
        public string? UserPwd { get; set; }

        /// <summary>
        /// 用户状态
        /// </summary>
        public int UserState { get; set; }

        public string UserStateName { get; set; }
    }


}

